package com.chauncy.DMS.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import util.List2Json1.List2Json1;

import com.chauncy.DMS.bean.ManagerInfoBean;
import com.chauncy.DMS.dao.ManagerDaoImplement;

public class SelectAllHealthInfoServlet extends HttpServlet {
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ManagerDaoImplement daoImplement = new ManagerDaoImplement();
		List<ManagerInfoBean> list = (List)request.getSession().getAttribute("managerInfoList");
		//楼栋号
		String buildings = list.get(0).getManager_buildings();
		Object[] param1 = {buildings};
		//接收重载参数
		String dorm_number = request.getParameter("Dorm_number");
		int page = 0;
		int limit = 0;
		try {
			page = Integer.parseInt(request.getParameter("page"));
			limit = Integer.parseInt(request.getParameter("limit"));
		} catch (Exception e) {
		
		}
		
		String sql = "select health_info_table.Health_info_number,health_info_table.Dorm_number,health_info_table.Deduction,health_info_table.The_Value,health_info_table.Floor_number,health_info_table.Grade,health_info_table.health_type,date_format(health_info_table.Att_time, '%Y-%m-%d') AS `Att_time`,health_info_table.Remarks from health_info_table where Buildings_number = ? ";
		List<Map<String,Object>> healthInfoList  = null;
		JSONObject healthInfoJson = null;
		
		//获取总卫生记录数
		String sqlCount = "select count(*) from ("+sql+") as healthCount";
		
		//当dorm_number != null 时为table 重载
		if(dorm_number != null && !dorm_number.equals("")){ 
			//分页查询语句 
			sql = sql +"AND Dorm_number = ?";
			String sqlPage = sql + " limit ?,?";
			//查询方法缺省值
			Object[] param = {buildings,dorm_number,((page-1)*limit),limit};
			//查询方法
			healthInfoList = daoImplement.query(sqlPage, param);
			//将list map 转 json 
			String sqlCou = "select count(*) from" +
					" ( select health_info_table.Health_info_number," +
					"health_info_table.Dorm_number," +
					"health_info_table.Deduction," +
					"health_info_table.The_Value," +
					"health_info_table.Floor_number," +
					"health_info_table.Grade," +
					"health_info_table.health_type," +
					"date_format(health_info_table.Att_time, '%Y-%m-%d') AS `Att_time`," +
					"health_info_table.Remarks" +
					" from health_info_table" +
					" where Buildings_number = ?" +
					" AND health_info_table.Dorm_number = '"+dorm_number+"') as healthCount";
			healthInfoJson = List2Json1.mapToJSON(healthInfoList, param1, sqlCou);
		}else{
			sql = sql;
			String sqlPage = sql + " limit ?,?";
			//查询方法缺省值
			Object[] param = {buildings,((page-1)*limit),limit};
			//查询方法
			healthInfoList = daoImplement.query(sqlPage, param);
			//将list map 转 json 
			healthInfoJson = List2Json1.mapToJSON(healthInfoList, param1, sqlCount);
		}
		
		response.getWriter().print(healthInfoJson);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	
	}

}
